package com.hxx.common.core.wcontour.util;

import org.geotools.data.DataStore;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.Feature;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

/**
 * Feature写入数据数据库
 *
 * @author hxd
 */
public class FeatureWriteDbUtil {

    /**
     * FeatureCollection 数据入库
     *
     * @param fc
     * @param tableName
     * @param datastore
     */
    public static boolean write(FeatureCollection fc, DataStore datastore, String tableName) {
        FeatureIterator iterator = null;
        try {
            if (!isExistsTable(datastore, tableName)) {
                createTable(fc, datastore, tableName);
            }
            iterator = fc.features();
            FeatureWriter<SimpleFeatureType, SimpleFeature> fw = datastore.getFeatureWriter(tableName,
                    Transaction.AUTO_COMMIT);
            while (iterator.hasNext()) {
                Feature feature = iterator.next();
                fw.hasNext();
                SimpleFeature simpleFeature = fw.next();
                Collection<Property> properties = feature.getProperties();
                Iterator<Property> propertyIterator = properties.iterator();
                while (propertyIterator.hasNext()) {
                    Property property = propertyIterator.next();
                    simpleFeature.setAttribute(property.getName().toString(), property.getValue());
                }
                fw.write();
            }
            fw.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            datastore.dispose();
            if (iterator != null) {
                iterator.close();
            }
        }
        return false;
    }

    /**
     * 判断表是否已存在
     *
     * @param datastore
     * @param tableName
     * @return
     */
    private static boolean isExistsTable(DataStore datastore, String tableName) {
        try {
            datastore.getSchema(tableName);
            return true;
        } catch (Exception e) {
            // 异常说明不存在该表
        }
        return false;
    }

    /**
     * 创建表
     *
     * @param fc
     * @param datastore
     * @param tableName
     */
    private static void createTable(FeatureCollection fc, DataStore datastore, String tableName) throws IOException {
        // 表已存在，但会覆盖原来的数据
        SimpleFeatureType simpleFeatureType = (SimpleFeatureType) fc.getSchema();
        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.init(simpleFeatureType);
        typeBuilder.setName(tableName);
        datastore.createSchema(typeBuilder.buildFeatureType());
    }
}
